<template>
  <div>
    <el-dialog :visible.sync="dialogFormVisible" top="10vh">
      <div class="content">
        <el-form ref="subform1" :model="form" :rules="rules">
          <!-- 标题-->
          <el-form-item label="弹窗名称" :label-width="formLabelWidth" prop="adName">
            <el-input v-model="form.adName" class="form" />
          </el-form-item>
          <!-- 省份选择 -->
          <el-form-item label="选择省份" :label-width="formLabelWidth" prop="provinceId">
            <el-select v-model="form.provinceId" placeholder="请选择" class="form" @change="changePro">
              <el-option
                v-for="item in provinceData"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <!-- 弹窗位置 -->
          <el-form-item label="弹窗位置" :label-width="formLabelWidth" prop="adType">
            <el-select v-model="form.adType" placeholder="请选择弹窗位置" class="form" :disabled="true">
              <el-option
                v-for="item in positionData"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="跳转类型" :label-width="formLabelWidth" prop="jumpType">
            <el-select
              v-model="form.jumpType"
              placeholder="请选择跳转位置"
              class="form"
              @change="changeType"
            >
              <el-option
                v-for="item in typeData"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <!-- 选择项目 -->
          <el-form-item
            label="选择项目"
            :label-width="formLabelWidth"
            prop="projectId"
            :hidden="proHidden"
          >
            <el-select v-model="form.projectId" placeholder="请选择项目" class="form">
              <el-option
                v-for="item in projectData"
                :key="item.id"
                :label="item.groupProjectName"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <!-- 选择礼包 -->
          <el-form-item
            label="选择礼包"
            :label-width="formLabelWidth"
            prop="goodsId"
            :hidden="goodsHidden"
          >
            <el-select v-model="form.goodsId" placeholder="请选择礼包" class="form">
              <el-option
                v-for="item in goodsData"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <!-- 对应小程序 -->
          <el-form-item label="对应小程序" :label-width="formLabelWidth" :hidden="appHidden">
            <el-select v-model="form.appletId" placeholder="请选择对应小程序" class="form">
              <el-option
                v-for="item in appletData"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <!-- 跳转链接 -->
          <el-form-item
            label="跳转链接"
            :label-width="formLabelWidth"
            prop="jumpUrl"
            :hidden="urlHidden"
          >
            <el-input v-model="form.jumpUrl" :disabled="isDisabled" class="form" />
          </el-form-item>
          <!-- 排序-->
          <el-form-item label="排序" :label-width="formLabelWidth" prop="sort">
            <el-input-number
              v-model="form.sort"
              :disabled="isDisabled"
              :min="1"
              style="width:150px; margin-right:16px; margin-bottom:10px;"
            />
          </el-form-item>
          <!-- 弹窗图片 -->
          <el-form-item label="弹窗图片" :label-width="formLabelWidth" prop="adPicture">
            <el-upload
              action="https://mpingweb.yuepinyuemei.com/api/upload"
              :data="{
                dirPath: 'authentication',
                upload_status: true }"
              :multiple="false"
              :limit="1"
              list-type="picture-card"
              :on-remove="handleRemove"
              :on-success="upload_main"
            >
              <i class="el-icon-plus" />
            </el-upload>
            <el-dialog>
              <img width="100%" :src="common.img_url+img_main" alt />
            </el-dialog>
            <img width="20%" :src="common.img_url+form.adPicture" alt />
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button plain @click="closeMask">取 消</el-button>
        <el-button v-if="!isDisabled&&infos&&!infos.id" type="primary" plain @click="submitInfo">确 定</el-button>
        <el-button v-if="!isDisabled&&infos&&infos.id" type="primary" plain @click="resetInfo">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getGoodsList } from "../../api/goods";
import { provinceProject } from "../../api/project";
import { appType } from "../../utils/constanst";
import { provinceList } from "../../api/province";
import { addAd, updateAd } from "../../api/adv";
export default {
  props: {
    isHidden: {
      type: Boolean,
      default: false,
    },
    detailInfo: {
      id: "",
    },
    isDisabled: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      formLabelWidth: "120px",
      dialogFormVisible: this.isHidden,
      img_main: "", // 图片
      infos: this.detailInfo, // 传过来的信息
      form: {
        adName: "", // 标题
        adPicture: undefined, // 封面图
        sort: 0, // 排序
        positionType: 13, // 广告位置- 弹窗

        jumpType: null, // 跳转类型
        jumpUrl: null, // 跳转链接
        projectId: "", // 项目id
        goodsId: "", // 礼包id
        appletId: "", // 小程序id
        adType: 0, // 弹窗位置
      },
      goodsData: [], // 礼包列表
      projectData: [], // 项目列表
      typeData: [
        { id: 0, name: "会员卡" },
        { id: 1, name: "项目详情" },
        { id: 2, name: "跳转链接" },
        { id: 3, name: "礼包详情" },
        { id: 4, name: "小程序" },
      ], // 跳转类型list
      positionData: [
        { id: 0, name: "首页" },
        // { id: 1, name: '个人' },
      ], // 位置list
      provinceData: [], // 省份list
      appletData: [], // 小程序list
      appHidden: true, // 小程序选择框是否隐藏-初始值：隐藏
      goodsHidden: true, // 礼包选择框是否隐藏-初始值：隐藏
      proHidden: true, // 项目详情选择框是否隐藏-初始值：隐藏
      urlHidden: true, // 跳转链接输入框是否隐藏-初始值：隐藏
      rules: {
        adName: [{ required: true, message: "请输入名称", trigger: "blur" }],
        provinceId: [
          {
            required: true,
            message: "请选择地址",
            trigger: "blur",
          },
        ],
        adType: [
          {
            required: true,
            message: "请选择弹窗位置",
            trigger: "blur",
          },
        ],
        jumpType: [
          {
            required: true,
            message: "请选择跳转类型",
            trigger: "blur",
          },
        ],
        jumpUrl: [
          {
            required: true,
            message: "请输入跳转链接",
            trigger: "blur",
          },
        ],
        projectId: [
          {
            required: true,
            message: "请选择项目",
            trigger: "blur",
          },
        ],
        goodsId: [
          {
            required: true,
            message: "请选择礼包",
            trigger: "blur",
          },
        ],
        appletId: [
          {
            required: true,
            message: "请选择小程序",
            trigger: "blur",
          },
        ],
        sort: [
          {
            required: true,
            message: "请输入排序",
            trigger: "blur",
          },
        ],
        adPicture: [
          {
            required: true,
            message: "图片不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  watch: {
    dialogFormVisible() {
      this.$emit("checkMask", this.dialogFormVisible);
    },
  },
  created() {
    // 跳转小程序，自己不能跳自己
    // 正式版省份id 全国-华视0 华悦36 plus71
    if (appType === 2) {
      this.appletData = [
        { id: 3, name: "华悦口腔" },
        { id: 4, name: "美分享" },
      ];
      this.form.provinceId = 0;
    }
    if (appType === 3) {
      this.appletData = [
        { id: 2, name: "华视优选" },
        { id: 4, name: "美分享" },
      ];
      this.form.provinceId = 36;
    }
    if (appType === 4) {
      this.appletData = [
        { id: 2, name: "华视优选" },
        { id: 3, name: "华悦口腔" },
      ];
      this.form.provinceId = 71;
    }
  },
  mounted() {
    // 获取省option
    provinceList({})
      .then((res) => {
        this.provinceData = res.data.data.list; // 将获取的省份option 赋值 provinceData
      })
      .catch((res) => {
        this.$message.error("网络连接错误，请稍后再试");
      });
    // 获取礼包列表
    getGoodsList({
      pageSize: 999,
      pageNum: 1,
      status: 1, // 上架
    })
      .then((res) => {
        this.goodsData = res.data.data.list;
      })
      .catch((res) => {
        this.$message.error("网络连接错误，请稍后再试");
      });
    // 说明是查看或者编辑 而不是添加
    if (this.infos && this.infos.id) {
      // 编辑状态-获取该对象中省份对应的项目
      provinceProject({
        id: this.infos.provinceId,
      })
        .then((res) => {
          this.form.project = res.data.data;
          this.projectData = res.data.data;
        })
        .catch((res) => {
          this.$message.error("网络连接错误，请稍后再试");
        });
      this.form.adName = this.infos.adName;
      this.form.sort = this.infos.sort;
      this.form.adPicture = this.infos.adPicture;
      this.form.provinceId = this.infos.provinceId;
      if (this.infos.jumpUrl.indexOf("card:1") === 0) {
        this.form.jumpType = 0;
      } else if (this.infos.jumpUrl.indexOf("id:") === 0) {
        this.form.jumpType = 1;
      } else if (this.infos.jumpUrl.indexOf("goods:") === 0) {
        this.form.jumpType = 3;
      } else if (this.infos.jumpUrl.indexOf("applet:") === 0) {
        this.form.jumpType = 4;
      } else {
        this.form.jumpType = 2;
      }
      this.form.projectId =
        this.infos.jumpUrl.indexOf("id:") === 0
          ? Number(this.infos.jumpUrl.substr(3, this.infos.jumpUrl.length - 3))
          : undefined;
      this.form.goodsId =
        this.infos.jumpUrl.indexOf("goods:") === 0
          ? Number(this.infos.jumpUrl.substr(6, this.infos.jumpUrl.length - 6))
          : undefined;
      this.form.appletId =
        this.infos.jumpUrl.indexOf("applet:") === 0
          ? Number(this.infos.jumpUrl.substr(7, this.infos.jumpUrl.length - 7))
          : undefined;
      this.form.jumpUrl = this.infos.jumpUrl;
      this.changeType(this.form.jumpType, 1);
    }
  },
  methods: {
    // 省份选择
    changePro(item) {
      this.form.provinceId = item;
      provinceProject({
        id: item,
      })
        .then((res) => {
          this.form.project = res.data.data;
          this.projectData = res.data.data;
        })
        .catch((res) => {
          this.$message.error("网络连接错误，请稍后再试");
        });
      this.form.projectId = ""; // 重置项目选择
    },
    // 跳转类型
    changeType(item, num) {
      this.form.jumpType = item;
      if (item === 0) {
        // 会员卡
        this.proHidden = true; // 项目详情选择框是否隐藏
        this.urlHidden = true; // 跳转链接输入框是否隐藏
        this.goodsHidden = true;
        this.appHidden = true;
      }
      if (item === 1) {
        // 项目详情
        this.proHidden = false;
        this.goodsHidden = true;
        this.urlHidden = true;
        this.appHidden = true;
        if (num !== 1) {
          this.form.projectId = undefined;
        }
      }
      if (item === 2) {
        // 跳转链接
        this.urlHidden = false;
        this.proHidden = true;
        this.goodsHidden = true;
        this.appHidden = true;
        if (num !== 1) {
          this.form.jumpUrl = null;
        }
      }
      if (item === 3) {
        // 礼包
        this.goodsHidden = false;
        this.proHidden = true;
        this.urlHidden = true;
        this.appHidden = true;
        if (num !== 1) {
          this.form.goodsId = undefined;
        }
      }
      if (item === 4) {
        // 小程序
        this.appHidden = false;
        this.goodsHidden = true;
        this.proHidden = true;
        this.urlHidden = true;
        if (num !== 1) {
          this.form.appletId = undefined;
        }
      }
    },
    // 添加
    submitInfo() {
      if (this.checkInfo()) {
        addAd({
          adName: this.form.adName,
          adPicture: this.img_main,
          jumpUrl: this.urlTyle(this.form.jumpUrl),
          sort: parseInt(this.form.sort),
          positionType: this.form.positionType, //
          provinceId: this.form.provinceId,
          adType: this.form.adType,
        })
          .then((res) => {
            if (res.data.code === 7001) {
              this.$message.error(res.data.msg);
            } else {
              this.$message({
                showClose: true,
                message: "添加" + res.data.msg,
                type: "success",
              });
              this.$emit("refresh", {});
            }
            this.closeMask();
          })
          .catch((res) => {
            this.$message({
              showClose: true,
              message: res.data.msg,
              type: "error",
            });
          });
      } else {
        // 弹出错误框
        this.$message({
          showClose: true,
          message: "请填写必要参数",
          type: "error",
        });
      }
    },
    // 修改
    resetInfo() {
      if (this.checkInfo()) {
        updateAd({
          id: this.infos.id,
          adName: this.form.adName,
          adPicture: this.img_main,
          jumpUrl: this.urlTyle(this.form.jumpUrl),
          sort: parseInt(this.form.sort),
          positionType: this.form.positionType, //
          provinceId: this.form.provinceId,
          adType: this.form.adType,
        })
          .then((res) => {
            if (res.data.code === 0) {
              this.$message({
                message: "修改成功",
                type: "success",
              });
              this.$emit("refresh", {});
            } else {
              this.$message.error(res.data.msg);
            }
            this.closeMask();
          })
          .catch((res) => {
            this.$message({
              showClose: true,
              message: res.data.msg,
              type: "error",
            });
          });
      } else {
        // 弹出错误框
        this.$message({
          showClose: true,
          message: "请填写必要参数",
          type: "error",
        });
      }
    },
    // 图片移除
    handleRemove() {
      this.img_main = "";
    },
    // 图片上传
    upload_main(res) {
      this.img_main = res.data;
    },
    closeMask() {
      this.$refs["subform1"].resetFields();
      this.dialogFormVisible = false;
      this.$emit("checkMask", this.dialogFormVisible);
    },
    // 检查参数
    checkInfo() {
      // 图片
      if (!this.img_main) {
        this.img_main = this.form.adPicture;
        return this.img_main !== undefined;
      }
      // 跳转类型不可为空
      if (this.form.jumpType === null) {
        return false;
      }
      // 项目选择不可为空
      if (this.form.jumpType === 1) {
        return this.form.projectId !== undefined;
      }
      // 跳转链接不可为空
      if (this.form.jumpType === 2) {
        return this.form.jumpUrl !== null;
      }
      // 礼包不可为空
      if (this.form.jumpType === 3) {
        return this.form.goodsId !== undefined;
      }
      // 小程序不可为空
      if (this.form.jumpType === 4) {
        return this.form.appletId !== undefined;
      }
      return !(!this.form.adName || !this.img_main);
    },

    // 跳转类型与最终赋值拼接
    urlTyle(e) {
      let jumpUrl = e;
      // 跳转类型-会员卡
      if (this.form.jumpType === 0) {
        jumpUrl = "card:1";
      }
      // 跳转类型-项目详情
      if (this.form.jumpType === 1) {
        jumpUrl = "id:" + this.form.projectId;
      }
      // 跳转类型-url
      if (this.form.jumpType === 2) {
        jumpUrl = this.form.jumpUrl;
      }
      // 跳转类型-礼包
      if (this.form.jumpType === 3) {
        jumpUrl = "goods:" + this.form.goodsId;
      }

      // 跳转类型-小程序
      if (this.form.jumpType === 4) {
        jumpUrl = "applet:" + this.form.appletId;
      }
      return jumpUrl;
    },
  },
};
</script>
<style  lang="scss" scoped>
.content {
  padding-right: 1%;
  height: 470px;
  max-height: 480px;
  overflow: auto;
  box-sizing: border-box;
}
.form {
  width: 400px;
  margin-right: 16px;
  margin-bottom: 10px;
}
.img-list {
  display: inline-block;
  vertical-align: middle;
  .img-item {
    width: 150px;
    height: 150px;
    border: 1px solid #ccc;
    display: inline-block;
    vertical-align: middle;
    overflow: hidden;
    margin-right: 10px;
    position: relative;
    .img {
      width: 100%;
    }
    .del-btn {
      width: 100%;
      height: 100%;
      background-color: rgba(0, 0, 0, 0.3);
      color: #fff;
      line-height: 150px;
      text-align: center;
      font-size: 40px;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      cursor: pointer;
      display: none;
    }
    &:hover {
      .del-btn {
        display: block;
      }
    }
  }
}
</style>
